<?php defined('SYSPATH') OR die('No direct access allowed.');

/**
 * Handles the nodes that have closing tags.  Contains a buffer
 * that holds the parsed text.
 */
abstract class Closingnode_Core
{
	// Buffer contents
	protected $buffer = '';
	// Node name
	protected $name = '';
	// Clause
	protected $clause = NULL;

	/**
	 * Sets the name of the node.
	 * 
	 * @param  string  Name of the node ($name)
	 */
	public function __construct($name = '')
	{
		$this->name = $name;
	}

	/**
	 * Adds text to the buffer.
	 * 
	 * @param  string  Buffer contents ($buffer)
	 */
	final public function add_text_to_buffer($buffer = '')
	{
		$this->buffer .= $buffer;
	}

	/**
	 * Parses the buffer.
	 * 
	 * @return  string  Buffer contents
	 */
	final public function parse_buffer()
	{
		return $this->parse_open() . $this->buffer . $this->parse_close();
	}

	/**
	 * Gets the node name.
	 * 
	 * @return  string  Name
	 */
	final public function get_name()
	{
		return $this->name;
	}
	
	/**
	 * Returns the output buffer
	 * 
	 * @return  string  Buffer contents
	 */
	final public function get_output_buffer()
	{
		return $this->buffer;
	}
	
	/**
	 * Pushes the clause into object property
	 * 
	 * @param  array  Tag array ($tag_parts)
	 */
	public function push_clause($tag_parts)
	{
		$this->clause = '';
		
		foreach ($tag_parts as $index => $tag_part)
		{
			$this->clause .= ' ' . $tag_part;
		}
		$this->clause = trim($this->clause);
	}

	/**
	 * Abstract function required by any classes that extend this one.
	 * Handles the parsing of the opening tag.
	 */
	abstract public function parse_open();
	
	/**
	 * Abstract function required by any classes that extend this one.
	 * Handles the parsing of the closing tag.
	 */
	abstract public function parse_close();
}